/**
*	bootstrap msg回复js模块
*   @Author jackson wang
*   @date 2016-09-022
*   @version 1.0
*
*/

define(["jquery","init","codeDef","bsResponseView","dataTable"], 
	function($,init,codeDef,bsResponseView,dataTable) {
	
	'use strict';
	
	//定义页面url
	var msgUrl = '/document/query/getPage.action?code=msgView';
	
	//回复页面url
	var responseUrl = '/document/query/getPage.action?code=responseView';
	
	
	var responseDetailUrl = '/document/query/getPage.action?code=responseDetail';
	
	//回复列表url
	var responseListUrl = '/docread/query/getBsListDomain.action';
	
	var msgQryUrl = '/document/query/getOne.action';
	
	var rootid = 'msg-view';
	
	var container = 'main-container';
	
	var openParam ;
	
	//当前数据
	var fData;
	
	//列表对象临时变量
	var dg_table;
	
	//列表选中行对象
	var selected = [];
	
	var public_method = {
		
		/**
		 * 打开js预览页面
		 * @param _options.container	容器对象
		 * @param _options.id			业务对象id
		 */
		openPage:function(_options){
			
			openParam = _options;
			private_method.openPage();
		}
		,
		
		/**
		 * 页面初始化方法
		 * @param 
		 */
		initPage:function(_options){
			
			var _df = $.extend({},openParam,_options);
			
			if(!_df.data){
				var _param = {
					url:'/'+init.getSite()+msgQryUrl,
					data:{id:_df.id},
					success:function(data){
						
						fData = data;
						private_method.initContent(data);
						
					}				
					,
					failure:function(){
	//					bsMsgServ.errorSlide('获取数据错误，请刷新页面重试。如问题依旧存在，请联系系统管理员');
						bsMsgServ.errorSlide(codeDef.getConfig({type:'load',code:'error'}));
					}
				}
				$.ajax(_param);
			}else{
				//缓存数据
				fData = _df.data;
				private_method.initContent(_df.data);
			}
			
			//绑定按钮事件
			var _btn = $('#' + rootid + ' .btn')
			if(_btn.length>0){
				_btn.unbind('click');
				_btn.bind('click',private_method.bindBtnEvent);			
			}
		}
		,
		
		/**
		 * 销毁页面
		 * 
		 */
		destroy:function(_options){
			fData = null;
			openParam = null;
		}
		
	}
	
	var private_method = {
		
		initContent:function(data){
			if(data){						
				
				if(window.console){
					console.log( 'init content ...')
				}
				if(data.archiveContent ==='1'){
					//显示附件内容为文档
					var _p = {
							url	: '/portal/getContentArchive',
							data:{docid:data.id,bizcode:'notification'},
							success:function(_data){
								
								var _content = '';
								if(_data && _data.rows ){
									
									if(_data && _data.rows ){
										if((_data.rows)[0]){
											var t = (_data.rows)[0]
											//文档内容										
											_content = t.content
											
										}
										
										
										
										//文档附件
										
										var _html ='<h4>';								
										$.each(_data.rows,function(i,o){
											var _tmp = '附件: <a class="archive-info" aid="'+o.id+'">'+o.fileName+'</a>';
											
											_html = _html?_html+'</br>'+_tmp:_tmp;									
										})												
														
										if(_html){
											_html = _html + '</h4>';
											_content +=_html;
										}
									}
															
									//文档内容
									var contentEl = $('#'+rootid + ' .content-text')
									if(contentEl.length==1){
										$(contentEl).html(_content)
									}									
								}
							}
							,
							error:function(){
								
							}
					}
					
					$.ajax(_p);
				}else{
					if(data.lobid){
						$.ajax({
							url:'/'+init.getSite()+'/lob/getOne.action',
							data:{id:data.lobid},
							success:function(data){
								if(data && data.lob_contents){
									//缓存文档
									fData.lob_contents = data.lob_contents;
									
									//文档内容
									var contentEl = $('#'+rootid + ' .content-text')
									if(contentEl.length==1){
										$(contentEl).html(data.lob_contents)
									}
								}
								
							}
							,
							failure:function(){
	//									bsMsgServ.errorSlide('获取数据错误，请刷新页面重试。如问题依旧存在，请联系系统管理员');
								bsMsgServ.errorSlide(codeDef.getConfig({type:'load',code:'error'}));
							}
						})
					}
				}
				
											
							
				if(data.name){
					//文档标题
					var contentHeaderEl = $('#'+rootid + ' .page-title')
					
					if(contentHeaderEl.length==1){
						$(contentHeaderEl).html(data.name)
					}
				}
				
				var _pageInfo = '';
//						if(data.typeName){
//							//文档类型
//							
//							_pageInfo +=data.typeName;
//
//						}															
							
				
				if(data.public_dept_name){
					_pageInfo += ' '+data.public_dept_name
					
				}
				
				if(data.public_user_name){
					_pageInfo += ' ' +data.public_user_name
				}							
						
				if(data.public_date){										
					
					_pageInfo += ' ' +data.public_date
				}													
				
				
				if(_pageInfo){
					//文档属性
					var contentHeaderEl = $('#'+rootid + ' .page-info')
					
					if(contentHeaderEl.length==1){
						$(contentHeaderEl).html(_pageInfo)
					}
				}
			}
		}
		,
		/**
		 * 打开js预览页面
		 * @param _options.data
		 */
		openPage:function(){
			private_method.loadContainerUrl(msgUrl);			
		}
		,
		
		/**
		 * 绑定按钮事件
		 */
		bindBtnEvent:function(evt){
			
			var btnConfig = {
				'response':btnEvent.response,
				'back':btnEvent.back,
				'responseDetail':btnEvent.responseDetail
			}
			init.applyBtnEvt({btnEvt:evt,btnConfig:btnConfig})
		}
		
		
		,
		/**
		 * 加载容器页面
		 */
		loadContainerUrl:function(url){
			
//			public_method.destroy();
			init.loadContainerUrl(url);
		}	
		
		
		
	}
	
	//按钮事件
	var btnEvent = {
		
		
		/**
		 * 回复页面
		 */
		response:function(evt){
			if(window.console){
				console.log('response fired...')	
			}
			bsResponseView.openPage(fData);
//			private_method.loadContainerUrl(responseUrl);			
		}
		,
		/**
		 * 返回页面
		 */
		back:function(evt){
			
			private_method.loadContainerUrl(backUrl);
		}
		,
		
		/**
		 * 查看回复
		 * 
		 * 
		 */
		responseDetail:function(evt){
//			private_method.loadContainerUrl(responseDetailUrl);	
			
			var _c = $('#' + rootid +' .content-text')
			if(_c.length>=1){
				_c.empty();
				
				_c.append('<table class="dg-list"></div>');
				
				var _param = codeDef.getConfig({type:'grid',code:'config'})
				
				_param.columns =[
    				{"data":null,"title":"序号"},
    				{ "data": "name","title":"回复标题"},
					{ "data": "modiDate","title":"回复时间"},
					{ "data": "user_deptname" ,"title":"科室名称"},
					{ "data":  "user_name","title":"回复人"},
					{ "data":  "lob_contents","title":"回复内容"},
					{ "data": "archive" ,"title":"附件"}
					//{ "当前环节": "status" },
					//{ "附件": "archive" }
    			]
				
    			_param.ajax = {
    				url:'/'+init.getSite()+responseListUrl,    				
    				type:'post',
    				"data": function ( d ) {  
		                  
		                //添加额外的参数传给服务器  
		                d.docid = fData.id;  
		            }
    			}
    			,
				_param.rowCallback = function( row, data ) {
		            if ( $.inArray(data.DT_RowId, selected) !== -1 ) {
		                $(row).addClass('selected');
		            }
		        }
				
    			if(dg_table){
    				dg_table.destroy();
    			}    			
    			dg_table =$('#'+rootid + ' .dg-list').DataTable(_param);
    			
    			dg_table.on('click', 'tr', function () {
			        var id = this.id;
			        var index = $.inArray(id, selected);			        
			 
			        if ( index === -1 ) {
			            selected.push( id );
			        } else {
			            selected.splice( index, 1 );
			        }
			 		
			        $(this).toggleClass('selected');
			        
			        
			        //funcDef.tableClick({bizcode:_options.module})
			        
			       
//			        funcDef.tableClick({bizcode:_options.module,id:id});
			        
			        
			    } );
			}
		}
	}
	
	return public_method;
})